package com.sgb.kjwl.viewmodel.power;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.MaterialFilterEntity;
import com.sgb.kjwl.model.entity.PowerEntity;
import com.sgb.kjwl.model.entity.PowerRequestEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.PowerRepertory;
import com.sgb.kjwl.view.ui.activity.power.PowerListActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PowerListModel extends BaseViewModel {

    public ObservableField<PowerRequestEntity> filterJson = new ObservableField<>(new PowerRequestEntity());
    private final PowerListActivity activity;
    private final PowerRepertory repertory;
    private int index = 1;

    public PowerListModel(PowerListActivity activity, PowerRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    /**
     * categoryNo：企业分类
     * typeNo：资质类型(默认给"")
     * levelNo：资质等级(默认给"")
     * recordNo：备案单位(默认给"")
     * areaNo：区域(1省内0省外，默认1)
     * "areaCode": "650000",
     * "pageNum": 1,
     * "pageSize": 10
     */
    public void getList(boolean isLoadMore) {
        if (isLoadMore) {
            index = index + 1;
        } else {
            index = 1;
        }
        filterJson.get().setAreaCode(repertory.getAreaCode());
        filterJson.get().setPageNum(index);
        filterJson.get().setPageSize(20);

        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPowerAPI().getPowerList(filterJson.get()).enqueue(new Callback<BaseEntity<ListResponse<List<PowerEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PowerEntity>>>> call, Response<BaseEntity<ListResponse<List<PowerEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState() != null && response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showList(response.body().getData().getList(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PowerEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTypeFilter() {
        APIManager.getInstance().getPowerAPI().getTypeFilter(filterJson.get().getCategoryNo()).enqueue(new Callback<BaseEntity<MaterialFilterEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MaterialFilterEntity>> call, Response<BaseEntity<MaterialFilterEntity>> response) {
                if (response.body() == null || response.body().getData() == null) return;
                activity.setTypeFilter(response.body().getData().getDatas());
            }

            @Override
            public void onFailure(Call<BaseEntity<MaterialFilterEntity>> call, Throwable t) {
                MLog.i("api", t.getMessage());

            }
        });

    }

    public void getRecordFilter() {

        APIManager.getInstance().getPowerAPI().getRecordFilter(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<MaterialFilterEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MaterialFilterEntity>>> call, Response<BaseEntity<List<MaterialFilterEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) return;
                activity.setRecordFilter(response.body().getData());

            }

            @Override
            public void onFailure(Call<BaseEntity<List<MaterialFilterEntity>>> call, Throwable t) {

            }
        });

    }

    public void getAreaFilter() {

        APIManager.getInstance().getPowerAPI().getAreaFilter().enqueue(new Callback<BaseEntity<List<MaterialFilterEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MaterialFilterEntity>>> call, Response<BaseEntity<List<MaterialFilterEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) return;
                activity.setAreaFilter(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MaterialFilterEntity>>> call, Throwable t) {

            }
        });

    }

}
